package com.gaga.console.base.config;

import javafx.application.Platform;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

@Configuration
public class JavaFxConfig {

    private static boolean toolkitInitialized = false;

    @PostConstruct
    public void initJavaFX() {
        if (!toolkitInitialized) {
            // 启动 JavaFX 工具包
            Platform.startup(() -> {
                // 空操作，仅用于初始化工具包
            });
            // 禁止自动退出
            Platform.setImplicitExit(false);
            toolkitInitialized = true;
        }
    }

    /**
     * 安全执行 JavaFX 操作
     */
    public static void runSafe(Runnable action) {
        if (Platform.isFxApplicationThread()) {
            action.run();
        } else {
            Platform.runLater(action);
        }
    }
}